/* 扩张和援建模块，不在框架内运行 */
/* 临时凑合 */
import  {harvest01} from '@/role/harvest'
import {debug01} from '@/role/debug'
import {vindicat01} from '@/role/vindicat'
import {GenerateAbility} from '@/utils'
/*
** 1型号扩张函数，支持1个claim爬虫和2个assist爬虫,其中分别负责升级控制器和建造虫卵
*/
export function expand_01(myRoom:string,disRoom:string):void
{
    var room_ = Game.rooms[myRoom]
    var disroom_ = Game.rooms[disRoom]
    var dis_ = new RoomPosition(25,25,disRoom)
    // 由spawn1生产所有类型爬虫
    var spawn1 = room_.find(FIND_MY_SPAWNS,{filter:(stru)=>{
        return !stru.spawning
    }})
    // if (spawn1.length <= 0) return
    var creepRole = [
        // {name:'worker-Lorry'+room_.name,ability:GenerateAbility(10,10,30)},
        //{name:'worker-Lili'+room_.name,ability:GenerateAbility(10,10,30)},
        // {name:'worker-Miku'+room_.name,ability:GenerateAbility(10,10,30)},
        // {name:'caption-Mike'+room_.name,ability:[TOUGH,TOUGH,TOUGH,MOVE,MOVE,MOVE,MOVE,CLAIM]},
    ]
    // 接下来判断该爬虫是否存在,不存在就孵化
    for (var i of creepRole)
    {
        if (disroom_ && disroom_.controller)
        if (disroom_.controller.my && i.name == 'caption-Mike'+room_.name)
            continue
        if (!Game.creeps[i.name] && spawn1.length > 0) spawn1[0].spawnCreep(i.ability,i.name)
    }
    // Mike的逻辑
    if (Game.creeps['caption-Mike'+room_.name])
    {

        var creep_mike = Game.creeps['caption-Mike'+room_.name] as Creep
        if (creep_mike.room.name != disRoom)
            creep_mike.farMoveTo(dis_,20 )
        else
        {
            if (creep_mike.claimController(creep_mike.room.controller) == ERR_NOT_IN_RANGE)
            {
                creep_mike.moveTo(creep_mike.room.controller)
            }
            if (creep_mike.room.controller.my)
            {
                var structure_ = creep_mike.room.find(FIND_HOSTILE_STRUCTURES) as Structure[]
                if (structure_.length > 0)
                    for (var str_ of structure_)
                        str_.destroy()
            }
        }
    }
    //Miku的逻辑
    if (Game.creeps['worker-Miku'+room_.name])
    {
        var creep_miku = Game.creeps['worker-Miku'+room_.name] as Creep
        if (creep_miku.room.name != disRoom)
        {
            creep_miku.farMoveTo(dis_,20 )
            creep_miku.memory.standed = true
        }
        else
        {
            // var source_1 = creep_miku.pos.findClosestByPath(FIND_SOURCES)
            debug01(creep_miku)
        }
    }
    if (Game.creeps['worker-Lorry'+room_.name])
    {
        
        var creep_lorry = Game.creeps['worker-Lorry'+room_.name] as Creep
        if (creep_lorry.room.name != disRoom)
        {
            creep_lorry.farMoveTo(dis_,20 )
            creep_lorry.memory.standed = true
        }
        else
        {
            creep_lorry.workstate(RESOURCE_ENERGY)
            if (creep_lorry.memory.working)
            {
                var construc_ = creep_lorry.pos.findClosestByPath(FIND_CONSTRUCTION_SITES)
                if (construc_)
                {
                    creep_lorry.build_(construc_)
                }
                else
                {
                    vindicat01(creep_lorry)
                }
            }
            else{
                const target = creep_lorry.pos.findClosestByRange(FIND_DROPPED_RESOURCES)
                if(target) {
                if(creep_lorry.pickup(target) == ERR_NOT_IN_RANGE) {
                    creep_lorry.moveTo(target)
                    }
                }
                else
                {
                    var source_2 = creep_lorry.pos.findClosestByPath(FIND_SOURCES)
                    creep_lorry.harvest__(source_2)
                }
            }
            
            //vindicat01(creep_lorry)
        }
    }
    if (Game.creeps['worker-Lili'+room_.name])
    {
        
        var creep_lili = Game.creeps['worker-Lili'+room_.name] as Creep
        if (creep_lili.room.name != disRoom)
        {
            creep_lili.farMoveTo(dis_,20 )
            creep_lili.memory.standed = true
        }
        else
        {
            harvest01(creep_lili)
        }
    }

}